import axios from 'axios'
import settings from '/settings.js'


import {
	Toast
} from 'vant'
// import store from '../store'

import store from '@/store'

import {
	getToken
} from '@/utils/auth'

// create an axios instance
const service = axios.create({
	baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
	withCredentials: true, // send cookies when cross-domain requests
	timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
	config => {
		if (store.state.user.token) {
			config.headers['token'] = getToken();
		}
		if (config.method == 'post') {
			config.data.platformCode = settings.platformCode;
		} else {
			config.params.platformCode = settings.platformCode;
		}
		return config
	},
	error => {
		// do something with request error
		console.log(error, 'err') // for debug
		return Promise.reject(error)
	}
)

service.interceptors.response.use(
	response => {
		if (response.statusCode == 302) {
			//登录过期
			store.commit('user/LOGOUT');
			return
		}
		return response.data
	},
	error => {
		Toast.fail({
			message: error.message,
			duration: 3000
		})
		return Promise.reject(error)
	}
)
export default service
